/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.eplatform.modules.sys.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.eplatform.common.service.TreeService;
import com.eplatform.modules.sys.dao.OfficeDao;
import com.eplatform.modules.sys.entity.Office;
import com.eplatform.modules.sys.utils.UserUtils;
import com.google.common.collect.Maps;

/**
 * 机构Service
 * @author ThinkGem
 * @version 2014-05-16
 */
@Service
@Transactional(readOnly = true)
public class OfficeService extends TreeService<OfficeDao, Office> {

	public List<Office> findAll(){
		return UserUtils.getOfficeList();
	}

	public List<Office> findList(Boolean isAll){
		if (isAll != null && isAll){
			return UserUtils.getOfficeAllList();
		}else{
			return UserUtils.getOfficeList();
		}
	}
	
	@Transactional(readOnly = true)
	public List<Office> findList(Office office){
		if(office != null){
			office.setParentIds(office.getParentIds()+"%");
			return dao.findByParentIdsLike(office);
		}
		return  new ArrayList<Office>();
	}
	
	@Transactional(readOnly = false)
	public void save(Office office) {
		super.save(office);
		UserUtils.removeCache(UserUtils.CACHE_OFFICE_LIST);
	}
	
	@Transactional(readOnly = false)
	public void delete(Office office) {
		super.delete(office);
		UserUtils.removeCache(UserUtils.CACHE_OFFICE_LIST);
	}
	/**
	 * 获取省级子集
	 * @param 类型 1地市，2协会
	 * @return
	 */
	public List<Office> getRootOffice(String type){
		Map params=Maps.newHashMap();
		params.put("parentId", "15");
		params.put("type", type);
		return dao.getRootOffice(params);
	}
	
	/**
	 * 获取下级协会或总会
	 * @param 类型 1总会，2协会
	 * @return
	 */
	public List<Map> getChildOffice(String parentid,String type){
		Map params=Maps.newHashMap();
		params.put("parentId", parentid);
		params.put("type", type);
		return dao.getChildOffice(params);
	}
	
	
	/**
	 * 获取所有父级协会体育总会
	 * @param parentids
	 * @return
	 */
	public List<Office> getAllParents(Office office){
		Map params=Maps.newHashMap();
		String[] ids=office.getParentIds().split(",");
		if(ids.length==0)
			return null;
		StringBuilder sb=new StringBuilder();
		for(String str : ids){
			sb.append("'"+str+"',");
		}
		sb.append("'0'");
		params.put("id", office.getId());//
		params.put("parentIds", sb.toString());
		return dao.getAllParents(params);
	}
	
}
